package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import com.hzit.util.MyBatisUtil;
import com.hzit.vo.StudentVo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.naming.CompositeName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StudentServiceImpl implements IStudentService {
	//1. 查询所有学生
	@Override
	public List<Student> findAll() throws IOException {
		//1.1 得到SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//1.2 开始查询学生并返回
		try(SqlSession sqlSession = sqlSessionFactory.openSession()){
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			return mapper.findAll();
		}
	}

	//2. 分页查询学生
	@Override
	public PageResult<StudentDTO> findByPage(int page, int pageSize) throws IOException {
		//2.1 分页参数设置
		PageHelper.startPage(page,pageSize);            // 分页插件提供的方法
		//2.2 开始查询
		//2.2.1 得到SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//2.2.2 开始查询学生并返回
		try(SqlSession sqlSession = sqlSessionFactory.openSession()){
			//① 查询所有学生
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
			List<Student> students = mapper.findAll();
			//② 转换为Page对象
			Page<Student> studentPage = (Page<Student>) students;
			List<Student> result = studentPage.getResult();
			// 定义每页的记录结果集合
			List<StudentDTO> studentDTOS = new ArrayList<>();
			// 循环添加记录到studentDTOS集合中
			for (Student student : result) {
				StudentDTO dto = new StudentDTO();
				BeanUtil.copyProperties(student,dto,true);
				// ③ 通过班级的id查询班级对象
				Classes classes = classesMapper.findById(student.getCid());
				// ④ 为dto设置班级名字
				dto.setCname(classes.getCname());
				// ⑤ 将当前得到的dto添加到studentDTOS集合中
				studentDTOS.add(dto);
			}
			//③ 返回pageResult对象
			return new PageResult<>(studentDTOS,studentPage.getTotal());
		}
	}

	//3. 分页带条件查询
	@Override
	public PageResult<StudentDTO> search(Integer page, Integer pageSize, StudentVo studentVo) throws IOException {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 得到SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession()){
			//3.3 得到mappr
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//3.4 开始封装查询条件
			List<Student> students = mapper.search(studentVo);
			//3.5 将上面的Page<Student>对象
			Page<Student> studentPage = (Page<Student>) students;
			//3.6 得到当前页的记录集合
			List<Student> result = studentPage.getResult();
			//3.7 将上面的List<Student>转换为List<StuentDTO>
			List<StudentDTO> collect = result.stream().map(m -> {
				StudentDTO dto = new StudentDTO();
				BeanUtil.copyProperties(m, dto, true);
				// 查询班级名称
				Classes classes = classesMapper.findById(m.getCid());
				// 为dto设置班级名称
				dto.setCname(classes.getCname());
				return dto;
			}).collect(Collectors.toList());
			//3.8 构造一个PageResult对象并返回
			return new PageResult<>(collect,studentPage.getTotal());
		}
	}

	//4. 保存学生
	@Override
	public void save(StudentDTO dto) throws IOException {
		//4.1 得到SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			Student student = new Student();
			BeanUtil.copyProperties(dto,student,true);
			mapper.save(student);
		}
	}

	//5. 修改学生
	@Override
	public void update(StudentDTO dto) throws IOException {
		//4.1 得到SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			Student student = new Student();
			BeanUtil.copyProperties(dto,student,true);
			mapper.update(student);
		}
	}
}
